package com.bobo.controller.back.report;

import com.alibaba.druid.util.StringUtils;
import com.bobo.annotations.RequestBody;
import com.bobo.annotations.RequestMapping;
import com.bobo.controller.base.BaseServlet;
import com.bobo.dto.Page;
import com.bobo.dto.ReportDto;
import com.bobo.model.Message;
import com.bobo.model.Report;
import com.bobo.service.message.IMessageService;
import com.bobo.service.message.MessageServiceImpl;
import com.bobo.service.report.IReportService;
import com.bobo.service.report.ReportServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by bobo on 16-11-3.
 */
@WebServlet(name = "Report",urlPatterns = {"/admin/report/*"})
public class ReportBack extends BaseServlet{

    IReportService reportService = new ReportServiceImpl();
    IMessageService messageService = new MessageServiceImpl();

    @RequestMapping("/getReports")
    public String getReports(HttpServletRequest request) {
        Page<ReportDto> page = reportService.getReportDtos(0, 0);
        request.setAttribute("page",page);
        return "jsp/back/report/getReports";
    }

    @RequestMapping("/getReport")
    public String getReport(HttpServletRequest request) {
        int current = 1;
        int pageSize = 5;

        int messageId = Integer.parseInt(request.getParameter("messageId"));

        Message message = messageService.getById(messageId);

        if (request.getParameter("current") != null && !StringUtils.equals(request.getParameter("current"), "")) {
            current = Integer.valueOf(request.getParameter("current"));
        }
        if (request.getParameter("pageSize") != null && !"".equals(request.getParameter("pageSize"))) {
            pageSize = Integer.valueOf(request.getParameter("pageSize"));
        }
        Page<Report> page = reportService.getReports(messageId, current, pageSize);

        request.setAttribute("message", message);
        request.setAttribute("page",page);
        return "jsp/back/report/getReport";
    }

    @RequestBody
    @RequestMapping("/del")
    public String del(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        reportService.del(id);
        return "true";
    }



}
